/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bitoon.basketdudes.ws;

import com.bitoon.basketdudes.jpa.PersistenceManager;
import com.bitoon.basketdudes.jpa.Reto;

import java.util.*;
import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

/**
 * @author PacoFELC
 * @version 12/04/2012
 */
public class DatosConfig {

    Logger log = Logger.getLogger(DatosEquipo.class);
    EntityManagerFactory emf = null;

    public DatosConfig() {

        try {
            // JPA Units
            emf = PersistenceManager.getInstance().getEntityManagerFactory("jpa_basket");
        } catch (Exception e) {
            log.error("[ERROR][constructor]: No se puedo conectar a la base de datos: " , e);
        }

    }

    public String wsConsultarRetoServidor ( HttpServletRequest request, HttpServletResponse response) {
        EntityManager   em          = null;
        XMLOutputter    out         = null;
        Document        doc         = null;
        int             error       = 0;
        short           id          = 0;

log.debug("[consultarRetoServidor Inicio]: " );

        try {
            em = emf.createEntityManager(); // check 07.08.2012

            /* componemos el xml */
            Element root=new Element("consultar_retos");
            root.setAttribute("err", new Integer(error).toString());

            Query q = em.createNamedQuery("Reto.findActivo");
            List<Reto> lista = q.getResultList();

            if (!lista.isEmpty()) {
                for ( int i=0;i< lista.size(); i++) {
                    Reto row = lista.get(i);

                    Element elemento = new Element("reto");
log.error("[ENCONTRADO]: "+ row.getId());
                    elemento.setAttribute("id",      String.valueOf(row.getId         ()));
                    //elemento.setAttribute("desc",    row.getDescripcion               () );
                    elemento.setAttribute("clas",    String.valueOf(row.getClase      ()));
                    elemento.setAttribute("difi",    String.valueOf(row.getDificultad ()));
                    elemento.setAttribute("juga",    String.valueOf(row.getJugadores  ()));
                    elemento.setAttribute("cuar",    String.valueOf(row.getCuartos    ()));
                    elemento.setAttribute("tipo",    String.valueOf(row.getTipo       ()));
                    elemento.setAttribute("cant",    String.valueOf(row.getCantidad   ()));
                    elemento.setAttribute("icon",    String.valueOf(row.getIcono      ()));
                    root.addContent(elemento);
                }
            }

            doc=new Document(root);
            doc.setDocType(null);
            out=new XMLOutputter();
        } catch (Exception e) {
            try { em.getTransaction().rollback(); } catch (Exception e2) { }
            log.error("[consultarRetoServidor]: " , e);
        } finally {
            try { em.close(); } catch (Exception e) { } // revisado 09/08/2012
        }
        return out.outputString(doc);
    }
}
